package com.zzyl.controller;

import com.zzyl.base.PageResponse;
import com.zzyl.base.ResponseResult;
import com.zzyl.dto.UserDto;
import com.zzyl.service.UserService;
import com.zzyl.vo.UserVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@Api(tags = "用户管理")
@RestController
@RequestMapping("/user")
public class UserController {
    @Autowired
    UserService userService;




    @PostMapping("/page/{pageNum}/{pageSize}")
    @ApiOperation(value = "用户分页",notes = "用户分页")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "userDto",value = "用户DTO对象",required = true,dataType = "userDto"),
            @ApiImplicitParam(paramType = "path",name = "pageNum",value = "页码",example = "1",dataType = "Integer"),
            @ApiImplicitParam(paramType = "path",name = "pageSize",value = "每页条数",example = "10",dataType = "Integer")
    })
    public ResponseResult<PageResponse<UserVo>> findUserVoPageResponse(
            @RequestBody UserDto userDto,
            @PathVariable("pageNum") int pageNum,
            @PathVariable("pageSize") int pageSize) {
        PageResponse<UserVo> pageResponse = userService.findUserPage(userDto, pageNum, pageSize);
        return ResponseResult.success(pageResponse);
    }

    @PutMapping
    @ApiOperation(value = "用户添加",notes = "用户添加")
    @ApiImplicitParam(name = "userDto",value = "用户DTO对象",required = true,dataType = "UserDto")
        public ResponseResult<UserVo> createUser(@RequestBody UserDto userDto) {
        log.info("用户新增信息:{}",userDto);
        UserVo  userVoResult = userService.createUser(userDto);
        return ResponseResult.success(userVoResult);
    }

    @PatchMapping
    @ApiOperation(value = "用户修改",notes = "用户修改")
    @ApiImplicitParam(name = "userDto",value = "用户DTO对象",required = true,dataType = "UserDto")
    public ResponseResult<Boolean> updateUser(@RequestBody UserDto userDto) {
        log.info("用户修改信息:{}",userDto);
        Boolean  flag = userService.updateUser(userDto);
        return ResponseResult.success(flag);
    }

    @DeleteMapping("/remove/{userIds}")
    @ApiOperation(value = "用户批量删除",notes = "用户批量删除")
    public ResponseResult deleteUserById(@PathVariable Long userIds) {
        log.info("用户根据id删除:{}",userIds);
        userService.deleteUserById(userIds);
        return ResponseResult.success();
    }
    @PutMapping("/is-enable/{id}/{status}")
    @ApiOperation(value = "用户启用与禁用",notes = "用户启用与禁用")
    public ResponseResult isEnableByIdUser(@PathVariable(name = "id") Long id ,@PathVariable(name = "status") String status) {
        log.info("用户启用与禁用:id:{}, status:{}",id,status);
        userService.isEnableByIdUser(id,status);
        return ResponseResult.success();
    }

    @PostMapping("/list")
    @ApiOperation(value = "查询所有用户",notes = "查询所有用户")
    @ApiImplicitParam(name = "userDto",value = "用户DTO对象",required = true,dataType = "UserDto")
    public ResponseResult<List<UserVo>> findAllUser(UserDto userDto) {
        log.info("查询所有用户:{}",userDto);
        List<UserVo> userVos = userService.findAllUser(userDto);
        return ResponseResult.success(userVos);
    }
}
